package com.lmn.actuator;

import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @describe: 自定义端点 暴漏此端点需要设置 management.endpoints.web.exposure.include=*
 */
/*
 * 自定义端点类
 * @Endpoint //表示这是一个自定义事件端点类
 * Endpoint 中有一个id  它是设置端点的URL路径  访问路径为：/actuator/{id}
 * */
@Endpoint(id = "systemtime", enableByDefault = true) //端点路径不要与系统自带的重合
@Component
public class MyEndpoint {
    // 模拟存储数据
    private final Map<String, String> dataStore = new ConcurrentHashMap<>();

    // @ReadOperation 对应GET请求

    /**
     * 请求示例：
     * GET http://localhost:1234/monitor/manage/systemtime/zhangsan/20
     *
     * @Selector  注解代表路径传参
     *
     * @param username
     * @param age
     *
     * @return
     */
    @ReadOperation
    public Map<String, Object> endpointByGet(@Selector String username,@Selector Integer age) {
        Map<String, Object> customMap = new HashMap<>();
        customMap.put("httpMethod", HttpMethod.GET.toString());
        customMap.put("username",username);
        customMap.put("age",age);
        return customMap;
    }


    // @WriteOperation 对应POST请求

    /**
     * 请求示例：
     * POST http://localhost:1234/monitor/manage/systemtime
     *
     * 请求参数为json格式
     *
     * {
     *     "username": "zhangsan",
     *     "age": 20
     * }
     *
     * @param username 参数都为必填项
     * @param age 参数都为必填项
     * @return
     */
    @WriteOperation
    public Map<String, Object> endpointByPost(String username,Integer age) {
        Map<String, Object> customMap = new HashMap<>();
        customMap.put("httpMethod", HttpMethod.POST.toString());
        customMap.put("username",username);
        customMap.put("age",age);
        return customMap;
    }


    // @DeleteOperation 对应Delete请求

    /**
     * 请求示例：
     * DELETE http://localhost:1234/monitor/manage/systemtime
     *
     * @return
     */
    @DeleteOperation
    public Map<String, Object> endpointByDelete() {
        Map<String, Object> customMap = new HashMap<>();
        customMap.put("httpMethod", HttpMethod.DELETE.toString());

        return customMap;
    }
}
